﻿/*
 * SpatialDbConnection
 * 
 * initial implementation : 20.9.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
//using Petra.Gis.DataServices;

namespace Petra.Gis.DataServices
{
  /// <summary>
  /// abstract template for any tabular style based spatial data storage
  /// note <c>Extension</c> collection, use it as Unbounded collection of any property that users might need
  /// </summary>
  public abstract partial class SpatialDbConnection : DbConnection
  {


    public virtual DataTable GetSpatialSchema()
    {
      return GetSpatialSchema(SpatialSchemaTables.Features);
    }





    public virtual DataTable GetSpatialSchema(string tableAlias)
    {
      return GetSpatialSchema(tableAlias, null);
    }


    //public abstract DataTable GetSpatialSchema(string tableAlias, string[] tableRowFilter);
    public virtual DataTable GetSpatialSchema(string tableAlias, string[] tableRowFilter)
    {

      if (this.State != ConnectionState.Open)
        throw new InvalidOperationException("cant perform on closed connection");

      //validate intput alias...
      switch (tableAlias)
      {
        case SpatialSchemaTables.Features:
          return getFeaturesInfo(tableRowFilter);
        case SpatialSchemaTables.FeaturesInfo:
          return getFeaturesInfo(tableRowFilter);
        case SpatialSchemaTables.Fields:
          return getFieldsInfo(tableRowFilter);
        case SpatialSchemaTables.CoordinateSystemsInfo:
          return getCsInfo(tableRowFilter);
        case SpatialSchemaTables.SpatialIndexes:
          return getSpatialIndexInfo(tableRowFilter);
        case SpatialSchemaTables.SpatialTileIndexesInfo:
          return getTileSpatialIndexInfo(tableRowFilter);
        case SpatialSchemaTables.SpatialQTreeIndexesInfo:
          return getQtreeSpatialIndexInfo(tableRowFilter);
        default:
          throw new ArgumentException("invalid table alias");
      }

    }




    protected abstract DataTable getFeaturesInfo(string[] tableRowFilter);
    protected abstract DataTable getFieldsInfo(string[] tableRowFilter);
    protected abstract DataTable getCsInfo(string[] tableRowFilter);
    protected abstract DataTable getSpatialIndexInfo(string[] tableRowFilter);
    protected abstract DataTable getTileSpatialIndexInfo(string[] tableRowFilter);
    protected abstract DataTable getQtreeSpatialIndexInfo(string[] tableRowFilter);




    protected string createFeatureInfoFilter(string[] rowFilterArray)
    {

      string filter = "";

      if (rowFilterArray.Length != 10)
        throw new ArgumentException("filter must be exact 10 elements array");

      // parse filter
      if (rowFilterArray[0] != null && rowFilterArray[0] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_CATALOG", rowFilterArray[0]);

      if (rowFilterArray[1] != null && rowFilterArray[1] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_SCHEMA", rowFilterArray[1]);

      if (rowFilterArray[2] != null && rowFilterArray[2] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_NAME", rowFilterArray[2]);

      if (rowFilterArray[3] != null && rowFilterArray[3] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_TYPE", rowFilterArray[3]);

      if (rowFilterArray[4] != null && rowFilterArray[4] != "")
        addParamToSimpleSqlWhere(ref filter, "TITLE", rowFilterArray[4]);

      if (rowFilterArray[5] != null && rowFilterArray[5] != "")
        addParamToSimpleSqlWhere(ref filter, "ABSTRACT", rowFilterArray[5]);

      if (rowFilterArray[6] != null && rowFilterArray[6] != "")
        addParamToSimpleSqlWhere(ref filter, "AUTHORITY", rowFilterArray[6]);

      if (rowFilterArray[7] != null && rowFilterArray[7] != "")
        addParamToSimpleSqlWhere(ref filter, "BOUNDS", rowFilterArray[7]);

      if (rowFilterArray[8] != null && rowFilterArray[8] != "")
        addParamToSimpleSqlWhere(ref filter, "CONTACTINFO", rowFilterArray[8]);

      if (rowFilterArray[9] != null && rowFilterArray[9] != "")
        addParamToSimpleSqlWhere(ref filter, "KEYWORDS", rowFilterArray[9]);

      return filter;
    }




    protected string createFieldsInfoFilter(string[] rowFilterArray)
    {

      string filter = "";

      if (rowFilterArray.Length != 7)
        throw new ArgumentException("filter must be exact 7 elements array");

      // parse filter
      if (rowFilterArray[0] != null && rowFilterArray[0] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_CATALOG", rowFilterArray[0]);

      if (rowFilterArray[1] != null && rowFilterArray[1] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_SCHEMA", rowFilterArray[1]);

      if (rowFilterArray[2] != null && rowFilterArray[2] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_NAME", rowFilterArray[2]);

      if (rowFilterArray[3] != null && rowFilterArray[3] != "")
        addParamToSimpleSqlWhere(ref filter, "FIELD_NAME", rowFilterArray[3]);

      if (rowFilterArray[4] != null && rowFilterArray[4] != "")
        addParamToSimpleSqlWhere(ref filter, "TITLE", rowFilterArray[4]);

      if (rowFilterArray[5] != null && rowFilterArray[5] != "")
        addParamToSimpleSqlWhere(ref filter, "DOMAIN_GEOMETRY_TYPE", rowFilterArray[5]);

      if (rowFilterArray[6] != null && rowFilterArray[6] != "")
        addParamToSimpleSqlWhere(ref filter, "COORDINATESYSTEM_ID", rowFilterArray[6]);

      return filter;
    }





    protected string createCoordSystemInfoFilter(string[] rowFilterArray)
    {
      string filter = "";
      //validate
      //if (rowFilterArray.Length != 2)
      //  throw new ArgumentException("filter must be exact 2 elements array");

      // parse filter
      if (rowFilterArray[0] != null && rowFilterArray[0] != "")
        addParamToSimpleSqlWhere(ref filter, "COORDINATESYSTEM_ID", rowFilterArray[0]);

      if (rowFilterArray[1] != null && rowFilterArray[1] != "")
        addParamToSimpleSqlWhere(ref filter, "COORDINATESYSTEM_INFO", rowFilterArray[1]);

      return filter;
    }



    protected string createSpatialIndexInfoFilter(string[] rowFilterArray)
    {
      string filter = "";

      if (rowFilterArray[0] != null && rowFilterArray[0] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_CATALOG", rowFilterArray[0]);

      if (rowFilterArray[1] != null && rowFilterArray[1] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_SCHEMA", rowFilterArray[1]);

      if (rowFilterArray[2] != null && rowFilterArray[2] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_NAME", rowFilterArray[2]);

      if (rowFilterArray[3] != null && rowFilterArray[3] != "")
        addParamToSimpleSqlWhere(ref filter, "FIELD_NAME", rowFilterArray[3]);

      if (rowFilterArray[3] != null && rowFilterArray[4] != "")
        addParamToSimpleSqlWhere(ref filter, "SPATIAL_INDEX_TYPE", rowFilterArray[4]);


      return filter;
    }




    protected string createTileSpatialIndexInfoFilter(string[] rowFilterArray)
    {

      string filter = "";

      //validate
      if (rowFilterArray[0] != null && rowFilterArray[0] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_CATALOG", rowFilterArray[0]);

      if (rowFilterArray[1] != null && rowFilterArray[1] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_SCHEMA", rowFilterArray[1]);

      if (rowFilterArray[2] != null && rowFilterArray[2] != "")
        addParamToSimpleSqlWhere(ref filter, "FEATURE_NAME", rowFilterArray[2]);

      if (rowFilterArray[3] != null && rowFilterArray[3] != "")
        addParamToSimpleSqlWhere(ref filter, "FIELD_NAME", rowFilterArray[3]);

      if (rowFilterArray[3] != null && rowFilterArray[4] != "")
        addParamToSimpleSqlWhere(ref filter, "TILE_FIELD_NAME", rowFilterArray[4]);

      if (rowFilterArray[3] != null && rowFilterArray[5] != "")
        addParamToSimpleSqlWhere(ref filter, "TILE_FN_NAME", rowFilterArray[5]);

      if (rowFilterArray[3] != null && rowFilterArray[6] != "")
        addParamToSimpleSqlWhere(ref filter, "FILTER_OPTIONS", rowFilterArray[6]);


      return filter;
    
    }




    protected void addParamToSimpleSqlWhere(ref string strOut, string paramName, string paramValue)
    {
      if (strOut != "")
        strOut = strOut + " AND " + paramName + " = '" + paramValue + "'";
      else
        strOut = strOut + paramName + " = '" + paramValue + "'";

    }



  }
}
